// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   RoleTxAction.java

package com.iphaeton.lms.action.role;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.organize.*;
import com.iphaeton.lms.business.role.IPrivilegeService;
import com.iphaeton.lms.business.role.IRoleService;
import com.iphaeton.lms.pojo.organize.Organize;
import com.iphaeton.lms.pojo.organize.User;
import com.iphaeton.lms.pojo.role.Role;
import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;

// Referenced classes of package com.iphaeton.lms.action.role:
//            RoleBizSupportAction

public class RoleTxAction extends RoleBizSupportAction
{

    public RoleTxAction()
    {
    }

    public ActionForward selectRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        int currentPage = 0;
        int pageSize = 10;
        String page = (String)daf.get("page");
        String row = request.getParameter("row");
        if(row == null)
            row = "";
        List r = null;
        try
        {
            String userId = ((UserSession)request.getSession().getAttribute("userSession")).getId();
            if(page == null || page.trim().equals(""))
                currentPage = 1;
            else
            if(page != null && !page.trim().equals(""))
                currentPage = Integer.parseInt(page);
            pageInfo = role.findRolePagination(userId, row, pageSize);
            pageInfo.setPage(currentPage);
            pageInfo.setRow(row);
            request.getSession().setAttribute("pageInfo", pageInfo);
            r = organize.findPageList(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("role", r);
        return mapping.findForward("roleList");
    }

    public ActionForward addRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        daf = (DynaActionForm)form;
        String name = (String)daf.get("name");
        String comment = (String)daf.get("comment");
        List privilegeList = null;
        Role newRole = new Role();
        newRole.setName(name);
        newRole.setComment(comment);
        try
        {
            String creater = ((UserSession)request.getSession().getAttribute("userSession")).getId();
            newRole.setCreater(creater);
            User u = user.findUserById(creater);
            newRole.setCname(u.getName());
            newRole = role.save(newRole);
            privilegeList = privilege.findPrivilege(0);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("privilege", privilegeList);
        request.setAttribute("role", newRole);
        return mapping.findForward("add_privilege");
    }

    public ActionForward roleInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String roleId = request.getParameter("roleId").trim();
        String page = request.getParameter("page");
        Role r = null;
        try
        {
            r = role.findRoleById(roleId);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("pageId", page);
        request.setAttribute("role", r);
        return mapping.findForward("roleEdit");
    }

    public ActionForward editRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String page = request.getParameter("page");
        String id = request.getParameter("id");
        String name = (String)daf.get("name");
        String comment = (String)daf.get("comment");
        Role r = null;
        try
        {
            r = role.findRoleById(id);
            r.setComment(comment);
            r.setName(name);
            r = role.save(r);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("pageId", page);
        request.setAttribute("type", "edit");
        request.setAttribute("role", r);
        return mapping.findForward("roleEditOk");
    }

    public ActionForward assignRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String userId = request.getParameter("userId");
        String roleId = request.getParameter("roleId");
        String organizeId = request.getParameter("organizeId");
        Role r = null;
        List list = new LinkedList();
        try
        {
            r = role.findRoleById(roleId);
            User u = user.findUserById(userId);
            list.add(u);
            if(organizeId == null || "".equals(organizeId))
                organizeId = u.getOrganizeId();
            role.setRole(r, u);
            group.setManageGroup(group.findGroupById(group.findManagerGroup(organizeId)), u);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("pageId", "1");
        request.setAttribute("type", "assign");
        request.setAttribute("role", r);
        request.setAttribute("user", list);
        return mapping.findForward("roleAssignOk");
    }

    public ActionForward reclaimRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 0;
        int pageSize = 10;
        String page = request.getParameter("page");
        String userId[] = request.getParameter("userId").split(";");
        String id = request.getParameter("id");
        Role r = null;
        Set cUser = new HashSet();
        try
        {
            r = role.findRoleById(id);
            for(int i = 0; i < userId.length; i++)
            {
                role.removeUser(r, userId[i]);
                cUser.add(user.findUserById(userId[i]));
            }

            currentPage = 1;
            pageInfo = role.findUserPagination(id, pageSize);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        pageInfo.setPage(currentPage);
        request.getSession().setAttribute("pageInfo", pageInfo);
        request.setAttribute("pageId", page);
        request.setAttribute("type", "reclaim");
        request.setAttribute("role", r);
        request.setAttribute("user", cUser);
        return mapping.findForward("roleAssignOk");
    }

    public ActionForward removeRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 0;
        int pageSize = 5;
        String page = request.getParameter("page");
        String row = request.getParameter("row");
        if(row == null)
            row = "";
        String roleId = request.getParameter("roleId");
        Role r = null;
        try
        {
            String userId = ((UserSession)request.getSession().getAttribute("userSession")).getId();
            r = role.findRoleById(roleId);
            if(r.getCreater().equals(userId))
                role.remove(r);
            currentPage = 1;
            pageInfo = role.findRolePagination(userId, row, pageSize);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        pageInfo.setPage(currentPage);
        pageInfo.setRow(row);
        request.getSession().setAttribute("pageInfo", pageInfo);
        request.setAttribute("pageId", page);
        request.setAttribute("type", "remove");
        request.setAttribute("role", r);
        return mapping.findForward("roleEditOk");
    }

    public ActionForward addPrivilege(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        String privilegeId[] = request.getParameter("id").split(";");
        String roleId = request.getParameter("roleId");
        Role r = null;
        try
        {
            r = role.findRoleById(roleId);
            for(int i = 0; i < privilegeId.length; i++)
            {
                com.iphaeton.lms.pojo.role.Privilege p = privilege.findPrivilegeById(privilegeId[i]);
                role.addPrivilege(r, p);
            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("role", r);
        return mapping.findForward("role_create_ok");
    }

    public ActionForward selectPrivilege(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 0;
        int pageSize = 5;
        String page = request.getParameter("page");
        String id = request.getParameter("roleId");
        List privilegeList = null;
        Role r = null;
        try
        {
            r = role.findRoleById(id);
            if(page == null || page.trim().equals(""))
                currentPage = 1;
            else
            if(page != null && !page.trim().equals(""))
                currentPage = Integer.parseInt(page);
            pageInfo = privilege.findPrivilegePagination(id, pageSize);
            pageInfo.setPage(currentPage);
            privilegeList = organize.findPageList(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.getSession().setAttribute("pageInfo", pageInfo);
        request.setAttribute("role", r);
        request.setAttribute("privilege", privilegeList);
        return mapping.findForward("role_detail");
    }

    public ActionForward assignPrivilege(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String roleId = request.getParameter("roleId");
        List privilegeList = null;
        Role r = null;
        try
        {
            r = role.findRoleById(roleId);
            privilegeList = privilege.findNotRolePrivilege(roleId);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("role", r);
        request.setAttribute("privilege", privilegeList);
        return mapping.findForward("add_privilege");
    }

    public ActionForward removePrivilege(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 0;
        int pageSize = 5;
        String page = request.getParameter("page");
        String id = request.getParameter("id");
        String roleId = request.getParameter("roleId");
        com.iphaeton.lms.pojo.role.Privilege p = null;
        List rolePrivilege = null;
        Role r = null;
        try
        {
            r = role.findRoleById(roleId);
            p = privilege.findPrivilegeById(id);
            role.removePrivilege(r, p);
            if(page != null || !"".equals(page))
                currentPage = Integer.parseInt(page);
            else
                currentPage = 1;
            pageInfo = privilege.findPrivilegePagination(roleId, pageSize);
            pageInfo.setPage(currentPage);
            rolePrivilege = organize.findPageList(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("pageId", page);
        request.setAttribute("role", r);
        request.setAttribute("privilege", rolePrivilege);
        return mapping.findForward("privilegeList");
    }

    public ActionForward selectUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 0;
        int pageSize = 10;
        String page = request.getParameter("page");
        String id = request.getParameter("id");
        Role r = null;
        List user = null;
        try
        {
            r = role.findRoleById(id);
            if(page == null || page.trim().equals(""))
                currentPage = 1;
            else
            if(page != null && !page.trim().equals(""))
                currentPage = Integer.parseInt(page);
            pageInfo = role.findUserPagination(id, pageSize);
            pageInfo.setPage(currentPage);
            request.getSession().setAttribute("pageInfo", pageInfo);
            user = organize.findPageList(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("role", r);
        request.setAttribute("user", user);
        return mapping.findForward("roleUserList");
    }

    public ActionForward getUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int currentPage = 0;
        int pageSize = 5;
        String page = request.getParameter("page");
        String row = request.getParameter("row");
        String roleId = request.getParameter("roleId");
        if(row == null || "".equals(row))
            row = "u.name";
        String url = null;
        List user = null;
        try
        {
            String orgId = ((UserSession)request.getSession().getAttribute("userSession")).getManageOrgId();
            url = organize.findOrganizeById(orgId).getUrl();
            if(page == null || page.trim().equals(""))
                currentPage = 1;
            else
            if(page != null && !page.trim().equals(""))
                currentPage = Integer.parseInt(page);
            pageInfo = role.findNotRoleUserPagination(url, roleId, row, pageSize);
            pageInfo.setPage(currentPage);
            pageInfo.setRow(row);
            request.getSession().setAttribute("pageInfo", pageInfo);
            user = organize.findPageList(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("user", user);
        request.setAttribute("roleId", roleId);
        return mapping.findForward("role_assign");
    }

    public ActionForward privilegeAssignPrepare(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String role_id = request.getParameter("role_id");
        if(role_id == null)
            role_id = "";
        List organizeList = null;
        List roleList = null;
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String organize_id = userSession.getManageOrgId();
            String user_id = userSession.getId();
            organizeList = organize.findAllChildOrganize(organize.findOrganizeById(organize_id));
            roleList = role.selectRole(user_id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("id", role_id);
        request.setAttribute("list", roleList);
        request.setAttribute("organize", organizeList);
        return mapping.findForward("privilege_assign");
    }

    public ActionForward getUserList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        List userList = null;
        String orgId = request.getParameter("id");
        try
        {
            if(orgId == null || "".equals(orgId))
                orgId = ((UserSession)request.getSession().getAttribute("userSession")).getManageOrgId();
            userList = organize.findUser(organize.findOrganizeById(orgId), 'a');
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("list", userList);
        return mapping.findForward("user_list");
    }

    private static Logger logger;
    private Pagination pageInfo;
    private DynaActionForm daf;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.role.RoleTxAction.class.getName());
    }
}
